﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UpdateApp
{
    public partial class Files : System.Web.UI.Page
    {
        protected string date;
        protected string path;
        protected string file = "";
        protected string alway = "";
        protected string temp = "";
        protected int flag = 1;
        
        protected Dictionary<string, string> list = new Dictionary<string, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Random rd = new Random();

            flag = rd.Next(1, 8);

            if (!IsPostBack)
            {
                file = Request["file"] ?? "Default";

                date = Request["date"] != null ? Request["date"] : "";

                alway = FileHelper.GetAlwayDirName().Replace(FileHelper.GetDirName() + "/", "");
                temp = FileHelper.GetTempDirName().Replace(FileHelper.GetDirName() + "/", "");

                path = date != "" ? FileHelper.GetDirName() + "/" + date : FileHelper.GetDirName();

                //将当前文件移入永久空间
                if (Request["alway"] != null && date != alway)
                {
                    //判断目标目录是否存在该文件
                    string alwayPath = Path.Combine(FileHelper.GetAlwayPath(), Request["alway"]);
                    if (File.Exists(alwayPath))
                        File.Delete(alwayPath);

                    File.Move(Path.Combine(FileHelper.GetUploadPath(), date, Request["alway"]), alwayPath);

                    Response.Redirect(Request.Url.ToString().Replace("&alway=" + Request["alway"], ""));
                }

                //将当前文件移入临时空间
                if (Request["temp"] != null && date != temp)
                {
                    //判断目标目录是否存在该文件
                    string tempPath = Path.Combine(FileHelper.GetTempPath(), Request["temp"]);
                    if (File.Exists(tempPath))
                        File.Delete(tempPath);

                    File.Move(Path.Combine(FileHelper.GetUploadPath(), date, Request["temp"]), tempPath);

                    Response.Redirect(Request.Url.ToString().Replace("&temp=" + Request["temp"], ""));
                }

                if (Request["name"] != null)
                {
                    string file = Path.Combine(FileHelper.GetUploadPath(), date, Request["name"]);

                    if (Request["Type"] != null && Request["Type"] == "2")
                    {
                        string dir = Path.Combine(FileHelper.GetUploadPath(), DateTime.Now.ToString("yyyy-MM-dd"), Path.GetFileNameWithoutExtension(file));

                        ZipHelper.UnZip(file, dir, null);

                        //查询html所在的目录
                        string dirName = FileHelper.GetFileDir(dir);

                        //直接打开目录
                        //dir = dirName.Replace(Path.GetDirectoryName(FileHelper.GetUploadPath()), "");
                        // \\Upload\\2021-08-06\\web\\web\\
                        //Response.Redirect(dir);

                        dir = dirName.Replace(FileHelper.GetUploadPath() + "\\", "");
                        // 2021-08-06\\web\\web\\

                        dir = HttpUtility.UrlEncode(dir);
                        Response.Redirect("Files.aspx?date=" + dir);
                    }
                    else
                    {
                        File.Delete(file);

                        Response.Redirect(Request.Url.ToString().Replace("&name=" + Request["name"], ""));
                    }
                }

                string Check = ConfigurationManager.AppSettings["Check"];

                //只允许白名单中的IP下载
                if (Check == "1")
                    if (!Common.CheckUserHost())
                        return;

                list = FileHelper.GetFiles(date);
            }
        }

        protected bool IsEdit(string file)
        {
            var arr = file.ToLower().Split('.');
            if (arr.Length != 2) return false;

            var list = new List<string>
            {
                "html",
                "txt",
                "ini"
            };

            return list.Contains(arr[1]);
        }
    }
}